﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace AWLoadTesterLibrary
{
    /// <summary>
    /// Class for saving and retrieving from the registry
    /// </summary>
    public class RegistryUtil
    {
        /// <summary>
        /// Registry Path
        /// </summary>
        static public string RegistryBase { get; set; }

        /// <summary>
        /// Registry key
        /// </summary>
        public RegistryKey MainKey { get; set; }

        /// <summary>
        /// Test URL
        /// </summary>
        public string URL { get; set; }

        /// <summary>
        /// Output file for compiled statistics
        /// </summary>
        public string OutputFile { get; set; }

        /// <summary>
        /// Browser concurrent connections
        /// </summary>
        public int ConConnections { get; set; }

        /// <summary>
        /// Number of minutes to run this test
        /// </summary>
        public int TestTime { get; set; }

        /// <summary>
        /// Number of seconds to possibly pause between following links
        /// </summary>
        public int BrowseTime { get; set; }

        /// <summary>
        /// Number of users to simulate every minute
        /// </summary>
        public int UserMins { get; set; }

        /// <summary>
        /// Indicates whether to show a browser window or not
        /// </summary>
        public int ShowBrowser { get; set; }

        /// <summary>
        /// Creates a RegistryUtil object.  This class manages registry access.
        /// </summary>
        public RegistryUtil()
        {
            RegistryBase = "AWLoadTester";

            URL = "http://bing.com/";
            OutputFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AWLoadTesterOutput.xml");
            ConConnections = 5;
            TestTime = 2;
            BrowseTime = 1;
            UserMins = 60;
            ShowBrowser = 0;

            MainKey = Registry.LocalMachine.OpenSubKey("Software", true);
            if (MainKey == null)
            {
                return;
            }

            MainKey = MainKey.CreateSubKey("AWLoadTester", RegistryKeyPermissionCheck.ReadWriteSubTree);
        }

        /// <summary>
        /// Read a registry key from memory by name
        /// </summary>
        /// <param name="name">The name of the item to read from the registry</param>
        /// <returns></returns>
        public object Read(string name)
        {
            object res = MainKey.GetValue(name, null);

            return res;
        }

        /// <summary>
        /// Read all the registry items into memory
        /// </summary>
        public void ReadAll()
        {
            URL = Read("URL") as String;
            OutputFile = Read("OutputFile") as String;

            try
            {
                TestTime = Convert.ToInt32(Read("TestTime"));
            }
            catch
            {
                TestTime = 0;
            }

            try
            {
                BrowseTime = Convert.ToInt32(Read("BrowseTime"));
            }
            catch
            {
                BrowseTime = 0;
            }

            try
            {
                ConConnections = Convert.ToInt32(Read("ConConnections"));
            }
            catch
            {
                ConConnections = 0;
            }

            try
            {
                UserMins = Convert.ToInt32(Read("UserMins"));
            }
            catch
            {
                UserMins = 0;
            }

            try
            {
                ShowBrowser = Convert.ToInt32(Read("ShowBrowser"));
            }
            catch
            {
                ShowBrowser = 0;
            }

            URL = URL ?? "";
            OutputFile = OutputFile ?? "";
        }

        /// <summary>
        /// Store a particular item into memory
        /// </summary>
        /// <param name="name">The item name</param>
        /// <param name="value">The item value</param>
        /// <param name="kind">The type of item being stored in the registry</param>
        public void Write(string name, object value, RegistryValueKind kind)
        {
            MainKey.SetValue(name, value, kind);
        }

        /// <summary>
        /// Store all items in memory into the registry
        /// </summary>
        public void WriteAll()
        {
            Write("URL", URL, RegistryValueKind.String);
            Write("OutputFile", OutputFile, RegistryValueKind.String);

            Write("TestTime", TestTime, RegistryValueKind.DWord);
            Write("BrowseTime", BrowseTime, RegistryValueKind.DWord);
            Write("ShowBrowser", ShowBrowser, RegistryValueKind.DWord);
            Write("ConConnections", ConConnections, RegistryValueKind.DWord);
            Write("UserMins", UserMins, RegistryValueKind.DWord);
        }
    }
}
